app.controller("login",function($scope,$http,$rootScope,$uibModal,get,Md5,$state,$cookies){
	 $scope.enterEvent = function(e) {
        var keycode = window.event?e.keyCode:e.which;
        if(keycode==13){
            $scope.playLogin();
        }
    }
	 
	 
	$scope.remember=false;
	
	 
 	if(window.event.keyCode == 13){
            $scope.playLogin();
        }
	
	$scope.playLogin=function(){
		
	
	/*	$http({    
			url:"http://192.168.0.209:8081/login",    
            method: "GET",
            headers: { 'Content-Type': 'application/x-www-form-urlencoded'},
            params: param,
            transformRequest: function(obj) {  
			     var str = [];  
			     for(var p in obj){  
			       str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));  
			     }  
			     return str.join("&");  
			   }  
			   }).then(function(e) {     
			   	localStorage.setItem("Authorization",e.data.data.Authorization)
				sessionStorage.setItem("userName",e.data.data.user.staff.sName)	
				sessionStorage.setItem("privileges",JSON.stringify(e.data.data.user.privileges))	
				window.location.href="index.html"
							console.info(e)
							
			}) */ 
		
		
		if($scope.remember) { //记住我
			var date = new Date();
			$scope.rememberLogin = {
				name: $scope.loginName,
				password: $scope.password,
				//          	time:120,
				date: date.getTime() + 604800000,
			}
			var rememberLogin = JSON.stringify($scope.rememberLogin)
			localStorage.setItem('rememberLogin', rememberLogin)
		}else{
			localStorage.clear()
		}
			var param={
			username:$scope.loginName,
			password:Md5.hex_md5($scope.password)
		}
		get.list("login",param).then(function(e){
			if(e.code!=null&&e.code==500){
					$scope.errorText=e.error
			}else{
				
				e.user.privileges.sort(function(a,b){
		            return a.pPlace - b.pPlace
		        })
				angular.forEach(e.user.privileges ,function(o){
					o.children.sort(function(a,b){
		            return a.pPlace - b.pPlace
		        })
				})
				console.log(e)
				localStorage.setItem("Authorization",e.Authorization)
				localStorage.setItem("user",JSON.stringify(e.user))	
				localStorage.setItem("privileges",JSON.stringify(e.user.privileges))	
				
				localStorage.setItem("Name","sq")
				localStorage.setItem("page",e.user.privileges[0].children[0].pRoute)
				var pIcon=e.user.privileges[0].pIcon
				var pRoute=e.user.privileges[0].children[0].pRoute;
				var s={}
				s[pIcon]=true
				s[pRoute]=false
				localStorage.setItem("pages",JSON.stringify(s))
				var myDate = new Date();
				localStorage.setItem("year",myDate.getFullYear())
				window.location.href="index.html"
			}
		})
	}
	
	
	var login = JSON.parse(localStorage.getItem('rememberLogin'))
	if(login) { //判断是否存在记录信息
		var date = (new Date()).getTime()
		if(login.date > date) {
			$scope.loginName = login.name
			$scope.password = login.password
			$scope.remember = true;
		} else {
			localStorage.clear();
		}
	}
		
})
